/*
 * netop.h
 *
 *  Created on: 2012-5-16
 *      Author: Administrator
 */

#include "stdafx.h"
#include <winsock2.h>

#ifndef NETOP_H_
#define NETOP_H_

#define NET_REMOTE_IP_ADDR_LEN 50

#define NET_MAX_TIMEOUT_COUNT 5

typedef enum
{
    E_NET_PROTOCOL_MIN = 0,
    E_NET_PROTOCOL_TCP = E_NET_PROTOCOL_MIN,
    E_NET_PROTOCOL_MAX,
    E_NET_PROTOCOL_BUTT,
} E_NET_PROTOCOLID;

typedef enum
{
    E_NET_MODE_MIN,
    E_NET_MODE_INITIATIVE = E_NET_MODE_MIN,
    E_NET_MODE_PASSIVE,
    E_NET_MODE_MAX,
    E_NET_MODE_BUTT,
} E_NET_MODEID;

typedef enum
{
    E_NET_CONNECTTYPE_MIN = 0,
    E_NET_CONNECTTYPE_PCCONTROL = E_NET_CONNECTTYPE_MIN,
    E_NET_CONNECTTYPE_PCDATA,
    E_NET_CONNECTTYPE_CAMERACONTROL,
    E_NET_CONNECTTYPE_CAMERADATA,
    E_NET_CONNECTTYPE_MAX,
    E_NET_CONNECTTYPE_BUTT,
} E_NET_CONNECTTYPEID;

typedef enum
{
    E_NET_CONNECT_TYPE_MIN,
    E_NET_CONNECT_TYPE_CLIENT = E_NET_CONNECT_TYPE_MIN,
    E_NET_CONNECT_TYPE_SERVER,
    E_NET_CONNECT_TYPE_MAX
} E_NET_CONNECT_TYPE;

typedef enum
{
    E_NET_CONNECT_STATUS_MIN,
    E_NET_CONNECT_STATUS_DISCONNECTED = E_NET_CONNECT_STATUS_MIN,
    E_NET_CONNECT_STATUS_CONNECTED,
    E_NET_CONNECT_STATUS_MAX,
} E_NET_CONNECT_STATUS;

typedef struct S_NET_CONNECT
{
    SOCKET listenFd;
    SOCKET sockFd;
    int conStatus;
    int conType;
    int localPort;
    char remoteIp[NET_REMOTE_IP_ADDR_LEN];
    int remotePort;
} S_NET_CONNECT;

int NetTcpClientConDesInit(S_NET_CONNECT *pConDes, char *pRemoteIp,
  int remotePort);
int NetTcpServerConDesInit(S_NET_CONNECT *pConDes, int localPort);
int NetTcpInit(S_NET_CONNECT *pConDes);
int NetTcpConnect(S_NET_CONNECT *pConDes);
int NetTcpSend(S_NET_CONNECT *pConDes, char *pBuf, int len);
int NetTcpRecv(S_NET_CONNECT *pConDes, char *pBuf, int len);
BOOL NetTcpRecvASync(S_NET_CONNECT *pConDes, char *pBuf, int iExlen,
  int *pRcvLen);
int NetTcpDisconnect(S_NET_CONNECT *pConDes);
int NetTcpDeInit(S_NET_CONNECT *pConDes);
int NetTcpGetConnectStatus(S_NET_CONNECT *pConDes);
void SetSockOptions(int fd);

#endif /* NETOP_H_ */
